<!DOCTYPE html>
<html lang="UTF-8" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <!-- jquery -->
        <script th:src="@{/js/jquery.min.js}" type="text/javascript"></script>
        <!-- bootstrap -->
        <link rel="stylesheet" th:href="@{/bootstrap/css/bootstrap.min.css}" type="text/css"/>
        <script th:src="@{/bootstrap/js/bootstrap.min.js}" type="text/javascript"></script>
        <!-- jquery-validator -->
        <script th:src="@{/jquery-validation/jquery.validate.min.js}" type="text/javascript"></script>
        <script th:src="@{/jquery-validation/localization/messages_zh.min.js}" type="text/javascript"></script>
        <!-- layer -->
        <script th:src="@{/layer/layer.js}" type="text/javascript"></script>
        <!-- md5.js -->
        <script th:src="@{/js/md5.min.js}" type="text/javascript"></script>
        <!-- common.js -->
        <script th:src="@{/js/common.js}" type="text/javascript"></script>
        <title>商品详情</title>
    </head>
    <body>
        <!-- 引入公共头部 -->
        <div class="panel panel-default">
            <div id="head" th:include="common/head :: common_head"></div>
        </div>
        <div class="panel panel-default">
            <div class="panel-heading">秒杀商品详情</div>
            <div class="panel-body">
                <span th:if="${user eq null}"> 您还没有登录，请登陆后再操作<br/></span>
                <span>默认收货地址：</span><span th:if="${userAddress eq null}">没有收货地址的提示。。。</span>
                <span th:if="${userAddress ne null}" th:text="${userAddress.name}"></span>
                <span th:if="${userAddress ne null}" th:text="${userAddress.userId}"></span>
                <span th:if="${userAddress ne null}" th:text="${userAddress.addressDetail}"></span>
            </div>
            <table class="table" id="goodslist">
                <tr>
                    <td>商品名称</td>
                    <td colspan="3" th:text="${goods.goodsName}"></td>
                </tr>
                <tr>
                    <td>商品图片</td>
                    <td colspan="3"><img th:src="@{${goods.goodsImage}}" width="200" height="200"/></td>
                </tr>
                <tr>
                    <td>秒杀开始时间</td>
                    <td th:text="${#dates.format(goods.startDate, 'yyyy-MM-dd HH:mm:ss')}"></td>
                    <td id="miaoshaTip">
                        <input type="hidden" id="remainSeconds" th:value="${remainSeconds}"/>
                        <span th:if="${miaoshaStatus eq 0}">秒杀倒计时：<span id="countDown" th:text="${remainSeconds}"></span>秒</span>
                        <span th:if="${miaoshaStatus eq 1}">秒杀进行中</span>
                        <span th:if="${miaoshaStatus eq 2}">秒杀已结束</span>
                    </td>
                    <td>
                        <form id="miaoshaForm" method="post" action="/miaosha/do/miaosha">
                            <button class="btn btn-primary btn-block" type="submit" id="buyButton">立即秒杀</button>
                            <!-- 隐藏的请求参数 -->
                            <input type="hidden" name="goodsId" th:value="${goods.id}"/>
                        </form>
                    </td>
                </tr>
                <tr>
                    <td>商品原价</td>
                    <td colspan="3" th:text="${goods.goodsPrice}"></td>
                </tr>
                <tr>
                    <td>秒杀价</td>
                    <td colspan="3" th:text="${goods.miaoshaPrice}"></td>
                </tr>
                <tr>
                    <td>库存数量</td>
                    <td colspan="3" th:text="${goods.stockCount}"></td>
                </tr>
            </table>
        </div>
    </body>
    <script>
        $(function () {
            countDown();
        });

        function countDown() {
            var remainSeconds = $("#remainSeconds").val();
            var timeOut;
            // 秒杀还没开始，倒计时
            if (remainSeconds > 0) {
                $("#buyButton").attr("disabled", true);
                // 计时器每隔一秒执行一次。
                timeOut = setTimeout(function () {
                    $("#countDown").text(remainSeconds - 1);
                    $("#remainSeconds").val(remainSeconds - 1);
                    countDown();
                }, 1000);
            }
            // 秒杀正在进行中
            else if (remainSeconds == 0) {
                $("#buyButton").attr("disabled", false);
                // 去掉倒计时
                if (timeOut) {
                    clearTimeout(timeOut);
                }
                $("#miaoshaTip").html("秒杀进行中")
            }
            // 秒杀已经结束
            else {
                $("#buyButton").attr("disabled", true);
                $("#miaoshaTip").html("秒杀已经结束")
            }
        }
    </script>
</html>
